#!/bin/sh

# create a d52 control file from the Keil symbol table and
# listing files.  d52 doesn't like multiply defined symbols, and
# the keil tables make it hard to distinguish local and global
# vars, so in some cases we include the keil "long" name for a
# symbol.  all in all, it's a half-assed job, but it's way better
# than nothing.

m51file=$1

test "$m51file" && : <$m51file || exit 1

(

 grep 'XDATA.*UNIT.*?.*?.*?' $m51file  |
    sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]\+/\t/g' |
    cut -f2,5 |
    sed -e 's/\. //g' \
	-e 's/\([[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]]\)H/0x\1/' \
	-e 's/?\(..\)?\(.*\)?\(.*\)/\1_\2_\3/' -e 's/?XD?/xd_/' -e 's/^/S /'

	# -e 's/?[^X].?\(.*\)?\(.*\)/\1_\2/' -e 's/?XD?/xd_/' -e 's/^/S /'

 egrep '(CODE.*UNIT.*?.*?.*?|CODE.*UNIT.*?[^?]*$)' $m51file  |
    sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]\+/\t/g' |
    cut -f2,5 |
    sed -e 's/\. //g' \
	-e 's/\([[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]]\)H/0x\1/' \
	-e 's/?..?\(.*\)?.*/\1/' \
	-e 's/?//' \
	-e 's/^/L /'

 grep 'C:.*PUBLIC' $m51file |
    sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]\+/\t/g' |
    cut -f 1,3 |
    sed -e 's/\([[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]]\)H/0x\1/' \
        -e 's/^C:/L /'

) | sort -u |
    awk '{
		if ( $1 != last_1 || $2 != last_2) {
			print
		# } else {
		# 	print "dropping" $3
		}
		last_1=$1
		last_2=$2
	}'

exit

for x in \
    $(grep 'XDATA.*ABSOLUTE' $m51file  |
	sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]\+/\t/g' |
	cut -f2)
do
    (cd lst; grep $x *.lst) | sed 's/\.lst:/ /'
done  |
    sed -e 's/\. //g' -e 's/\([[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]]\)H/0x\1/' |
    awk '{print "S", $6, $2 "__" $1}' 

